package leetcode.code0847;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int shortestPathLength(int[][] graph) {
		int n = graph.length;
		if (n == 1) {
			return 0;
		}
		boolean[][] been = new boolean[n][1 << n];
		Queue<int[]> queue = new LinkedList<>();
		for (int i = 0; i < n; i++) {
			int mask = 1 << i;
			been[i][mask] = true;
			queue.add(new int[] { i, mask, 0 });
		}
		int limit = (1 << n) - 1;
		while (!queue.isEmpty()) {
			int[] cur = queue.poll();
			int i = cur[0], mask = cur[1], dist = cur[2];
			int nextDist = dist + 1;
			for (int nexti : graph[i]) {
				int nextMask = mask | 1 << nexti;
				if (nextMask == limit) {
					return nextDist;
				}
				if (!been[nexti][nextMask]) {
					been[nexti][nextMask] = true;
					queue.add(new int[] { nexti, nextMask, nextDist });
				}
			}
		}
		return 0;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
